package com.fw.payment.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.payment.service.ServiceExecutor;
import com.fw.payment.service.ServiceOrder;
import com.fw.service.bean.ErrorBean;
import com.fw.service.passwordpolicy.PasswordPolicyService;
import com.fw.service.passwordpolicy.bean.PasswordPolicyBean;

@Component
@Scope(SCOPE_PROTOTYPE)
public class PasswordPolicyBAO extends AbstractBAO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private PasswordPolicyService passwordPolicyService;

    public ErrorBean validateInputPassword(String password) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.passwordPolicyService);
	    serviceOrder.setMethodName("validateInputPassword");
	    serviceOrder.setParameterTypes(new Class[] { String.class });
	    serviceOrder.setParameterValues(new Object[] { password });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		ErrorBean errorBean = (ErrorBean) serviceOrder.getReturnValue();
		return errorBean;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public List<ErrorBean> validatePasswordPolicy(String password) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.passwordPolicyService);
	    serviceOrder.setMethodName("validatePasswordPolicy");
	    serviceOrder.setParameterTypes(new Class[] { String.class });
	    serviceOrder.setParameterValues(new Object[] { password });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<ErrorBean> errorBeans = (List<ErrorBean>) serviceOrder.getReturnValue();
		return errorBeans;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public PasswordPolicyBean getPasswordPolicy() throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.passwordPolicyService);
	    serviceOrder.setMethodName("getPasswordPolicy");
	    serviceOrder.setParameterTypes(new Class[] {});
	    serviceOrder.setParameterValues(new Object[] {});

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		PasswordPolicyBean passwordPolicyBean = (PasswordPolicyBean) serviceOrder.getReturnValue();
		return passwordPolicyBean;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public PasswordPolicyBean setPasswordPolicy(PasswordPolicyBean passwordPolicyBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.passwordPolicyService);
	    serviceOrder.setMethodName("setPasswordPolicy");
	    serviceOrder.setParameterTypes(new Class[] { PasswordPolicyBean.class });
	    serviceOrder.setParameterValues(new Object[] { passwordPolicyBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		PasswordPolicyBean bean = (PasswordPolicyBean) serviceOrder.getReturnValue();
		return bean;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public String generatePassword(Boolean shuffle) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.passwordPolicyService);
	    serviceOrder.setMethodName("generatePassword");
	    serviceOrder.setParameterTypes(new Class[] { Boolean.class });
	    serviceOrder.setParameterValues(new Object[] { shuffle });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		String password = (String) serviceOrder.getReturnValue();
		return password;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

}
